package com.patelski.magic.model;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Entity;

import org.jboss.seam.annotations.Name;

public enum Color {

	W("W", "White"),
	U("U", "Blue"),
	B("B", "Black"),
	R("R", "Red"),
	G("G", "Green"),
	Art("Art", "Artifact"),
	Lnd("Lnd", "Land"),
	Pln("Pln", "Plane"),
	Gld("Gld", "Gold"),
	HWU("HWU", "Hybrid: White / Blue"),
	HUB("HUB", "Hybrid: Blue / Black"),
	HBR("HBR", "Hybrid: Black / Red"),
	HRG("HRG", "Hybrid: Red / Green"),
	HWG("HWG", "Hybrid: White / Green"),
	HWB("HWB", "Hybrid: White / Black"),
	HWR("HWR", "Hybrid: White / Red"),
	HUR("HUR", "Hybrid: Blue / Red"),
	HUG("HUG", "Hybrid: Blue / Green"),
	HBG("HBG", "Hybrid: Black / Green"),
	HGLD("HGLD", "Hybrid: Gold"),
	SWU("SWU", "Split: White / Blue"),
	SUB("SUB", "Split: Blue / Black"),
	SBR("SBR", "Split: Black / Red"),
	SRG("SRG", "Split: Red / Green"),
	SWG("SWG", "Split: White / Green"),
	SWB("SWB", "Split: White / Black"),
	SWR("SWR", "Split: White / Red"),
	SUR("SUR", "Split: Blue / Red"),
	SUG("SUG", "Split: Blue / Green"),
	SBG("SBG", "Split: Black / Green"),
	SGLD("SGLD", "Split: Gold"),
	SR("SR", "Split: Red");

    private String value;
    private String description;

    Color(String value, String description) {
        this.value = value;
        this.description = description;
    }
    
    
    public static Color parseCastingCost(String castingCost) {
        boolean white = false, blue = false, black = false, red = false, green = false, 
            hybrid = false, gold = false, artifact = false;
        int colors = 0;
        
        if (null == castingCost || "".equals(castingCost)) {
        	// Either a land or a Plane (from Planechase)
        	// if (Card.getType() starts with "Plane") {
        	//	return Color.Pln;
        	// }
            return Color.Lnd;
        }
        if (castingCost.contains("W")) {
            white = true;
            colors++;
        }
        if (castingCost.contains("U")) {
            blue = true;
            colors++;
        }
        if (castingCost.contains("B")) {
            black = true;
            colors++;
        }
        if (castingCost.contains("R")) {
            red = true;
            colors++;
        }
        if (castingCost.contains("G")) {
            green = true;
            colors++;
        }
        if (castingCost.contains("{")) {
            hybrid = true;
        }
        if (hybrid && colors == 2) {
            // its a classic hybrid
            if (white && blue) {
                return Color.HWU;
            }
            if (white && blue) {
                return Color.HWU;
            }
            if (white && black) {
                return Color.HWB;
            }
            if (white && red) {
                return Color.HWR;
            }
            if (white && green) {
                return Color.HWG;
            }
            if (blue && black) {
                return Color.HUB;
            }
            if (blue && red) {
                return Color.HUR;
            }
            if (blue && green) {
                return Color.HUG;
            }
            if (black && red) {
                return Color.HBR;
            }
            if (black && green) {
                return Color.HBG;
            }
            if (red && green) {
                return Color.HRG;
            }
        }
        if (hybrid && colors > 2) {
        	return Color.HGLD;
        }
        if (colors > 1) {
            return Color.Gld;
        }
        if (colors == 1) {
        	if (white) {
        		return Color.W;
        	}
        	if (blue) {
        		return Color.U;
        	}
        	if (black) {
        		return Color.B;
        	}
        	if (red) {
        		return Color.R;
        	}
        	if (green) {
        		return Color.G;
        	}
        }
        
        // If parsable to an Integer, then it's an artifact
        try {
            new Integer(castingCost);
        } catch (NumberFormatException e) {
            return Color.Art;
        }
        // or if it would contain only numbers and x's:
        // TODO: Some regex test.
        return Color.Art;
        
    }
    
    
    
    /**
     * Return the Color based on the String value
     * @return Color enum object
     */
    public static Color fromString(String value)
    {
    	return parseCastingCost(value);
//        return valueOf(value);
    }
    
    /**
     * Return a Collection of all literal values for this enumeration
     * @return java.util.Collection literal values
     */
    public static Collection<String> literals()
    {
        final Collection<String> literals = new ArrayList<String>(values().length);
        for (int i = 0; i < values().length; i++)
        {
            literals.add(values()[i].name());
        }
        return literals;
    }
    
	public String getDescription() {
		return description;
	}


	public String getValue() {
    	return value;
    }
	
}
